#include "widgethistogramme.h"
#include "ui_widgethistogramme.h"
#include "mainwindow.h"
#include "fenetreimage.h"
using namespace std;

WidgetHistogramme::WidgetHistogramme(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::WidgetHistogramme)
{
    mw = (MainWindow*) parent;
    ui->setupUi(this);

    boolcouleur[0]= ui->cbrouge->isChecked();
    boolcouleur[1]= ui->cbvert->isChecked();
    boolcouleur[2]= ui->cbbleu->isChecked();

    QObject::connect(ui->cbrouge, SIGNAL(clicked()), this, SLOT(cbSelection()));
    QObject::connect(ui->cbvert, SIGNAL(clicked()), this, SLOT(cbSelection()));
    QObject::connect(ui->cbbleu, SIGNAL(clicked()), this, SLOT(cbSelection()));

    typehisto=ui->comboBox->currentIndex();

    recadrageMin = 0;
    recadrageMin = 0;

}

WidgetHistogramme::~WidgetHistogramme()
{
    delete ui;
}

void WidgetHistogramme::cbSelection() {
    boolcouleur[0]= ui->cbrouge->isChecked();
    boolcouleur[1]= ui->cbvert->isChecked();
    boolcouleur[2]= ui->cbbleu->isChecked();
    this->repaint();
}


void WidgetHistogramme::paintEvent(QPaintEvent *)
{
    //si il n'y a plus de fenetre image on efface l'histogramme
    if(mw->getMdiArea()->activeSubWindow() == 0 ){
        this->setEnabled(false);
        return;
    }
    this->setEnabled(true);
    //sinon on le dessine
    FenetreImage *fi = (FenetreImage*) mw->getMdiArea()->activeSubWindow()->widget();
    if(fi != NULL && ! fi->vectCalques.empty()) {



        if(typehisto == kRgb) {
            std::vector<std::vector<int> >* histo;

            histo=  fi->getCalqueCourant()->getHistogrammeRgb();
            int max =0;
            unsigned int i,j;
            for(i=0;i<histo->size();i++) {
                for(j=0;j<histo->at(i).size();j++) {
                    if(max < histo->at(i).at(j)) {
                        max = histo->at(i).at(j);
                    }
                }
            }
            int hauteur = ui->whisto->height();
            int largeur = ui->whisto->width();

            int decalV = (this->height()-ui->whisto->height())/2;
            qreal xnext= (qreal)largeur / 256.0;
            qreal facteurY= (qreal)max/hauteur;
            qreal ycourant, xcourant=0;

            QPainter p(this);
            QPen pen;
            pen.setJoinStyle(Qt::RoundJoin);
            pen.setCapStyle(Qt::RoundCap);
            pen.setWidth(2);
            pen.setColor(QColor(255,0,0));
            p.setPen(pen);
            for(i=0;i<histo->size();i++) {
                j=0;
                if(boolcouleur[i]) {
                    for(j=0;j<histo->at(i).size();j++) {
                        ycourant = (histo->at(i)[j]/facteurY);
                        QRectF rect = QRectF(xcourant,hauteur+decalV, xnext, -ycourant);
                        p.drawRect(rect);
                        xcourant += xnext;
                    }
                }
                xcourant=0;
                if(i==0) {
                    pen.setColor(QColor(0,255,0));
                    p.setPen(pen);
                } else if(i==1) {
                    pen.setColor(QColor(0,0,255));
                    p.setPen(pen);
                }
            }
        } else {
            //YUV

            std::vector<std::vector<double> >* histo;
            histo = fi->getCalqueCourant()->getHistogrammeYuv();
            int max =0, cumul=0;
            unsigned int i,j;
            for(i=0;i<histo->size();i++) {
                for(j=0;j<histo->at(i).size();j++) {
                    if(boolcouleur[i]){
                        if(max < histo->at(i).at(j)) {
                            max = histo->at(i).at(j);
                        }
                    }
                    if(i==1 && boolcouleur[1]){
                        //cumule
                        cumul += histo->at(i).at(j);
                    }
                }
            }
            cout << max << "  " << cumul << endl;
            cumul>max?max=cumul:false;
            int hauteur = ui->whisto->height();
            int largeur = ui->whisto->width();

            int decalV = (this->height()-ui->whisto->height())/2;
            qreal xnext= (qreal)largeur / 256.0;
            qreal facteurY= (qreal)max/hauteur;
            qreal ycourant, xcourant=0;

            QPainter p(this);
            QPen pen;
            pen.setJoinStyle(Qt::RoundJoin);
            pen.setCapStyle(Qt::RoundCap);
            pen.setWidth(2);
            pen.setColor(QColor(255,0,0));
            p.setPen(pen);

            //Le Y
            i=0;
            for(j=1;j<histo->at(i).size();j++) {
                ycourant = (histo->at(i)[j]/facteurY);
                QRectF rect = QRectF(xcourant,hauteur+decalV, xnext, -ycourant);
                p.drawRect(rect);
                xcourant += xnext;
            }

            //Le cumulé
            if(boolcouleur[1]){
                pen.setColor(QColor(0,0,0));
                p.setPen(pen);
                int nb=0;
                xcourant=0;
                for(j=1;j<histo->at(1).size();j++) {
                    nb+=histo->at(i)[j];
                    ycourant = (nb/facteurY);
                    QRectF rect = QRectF(xcourant,hauteur+decalV, xnext, -ycourant);
                    p.drawRect(rect);
                    xcourant += xnext;
                }
            }
        }
    }
}

void WidgetHistogramme::on_comboBox_currentIndexChanged(int index)
{
    if(index == 0) {
        typehisto=index;
        //si RGB
        ui->cbrouge->setText("R");
        ui->cbvert->setText("V");


        ui->cbbleu->setEnabled(true);
        ui->cbbleu->setText("B");
    } else {
        typehisto=index;
        //si YUV
        ui->cbrouge->setText("Y");

        ui->cbvert->setText("cummule");
        //ui->cbvert->setText("U");

        ui->cbbleu->setEnabled(false);
        //ui->cbbleu->setText("V");
    }
    this->repaint();
}


//egaliser
void WidgetHistogramme::on_pushButton_released()
{
    FenetreImage* fi = (FenetreImage*) mw->getMdiArea()->activeSubWindow()->widget();
    fi->getCalqueCourant()->egaliserHistogramme();
    fi->updateCalqueCourant();
}

void WidgetHistogramme::on_recMax_valueChanged(int value)
{
    recadrageMax = value;
}

void WidgetHistogramme::on_recMin_valueChanged(int value)
{
    recadrageMin = value;
}

void WidgetHistogramme::on_recadrage_clicked()
{
    FenetreImage *fi = (FenetreImage*) mw->getMdiArea()->activeSubWindow()->widget();
    Calque* calque = fi->getCalqueCourant();
    calque->recadrageHistogramme( recadrageMin, recadrageMax);
    fi->updateCalqueCourant();
}
